import React, { Suspense, useEffect, useState } from "react";
import { BrowserRouter, useLocation, useNavigate, useRoutes } from "react-router-dom";
import { routes } from "./router/routes";
import { themeContext } from "./context";
import { ConfigProvider } from "antd";
const RouterView = () => useRoutes(routes);
const hours = new Date().getHours();
console.log(hours);
function App() {
  const [theme, setTheme] = useState(hours >= 11 ? "dark" : "light"); // dark | light
  const { pathname } = useLocation()
  const token = localStorage.getItem("token");
  const navigate = useNavigate()

  useEffect(() => {
    if(!token && pathname !== '/login') {
      console.log(123)
      navigate('/login')
    }
  }, [pathname])
  return (
    <themeContext.Provider value={{ theme, setTheme }}>
      <ConfigProvider
        theme={{
          components: {
            Layout: {
              // 顶部
              headerBg: theme === "light" ? "#fff" : "#001529",
              headerColor: theme === "light" ? "#000" : "#fff",
              //侧边栏
              siderBg: theme === "light" ? "#fff" : "#001529",
            },
          },
        }}
      >
        <Suspense>
          <RouterView></RouterView>
        </Suspense>
      </ConfigProvider>
    </themeContext.Provider>
  );
}

export default App;
